/**
 * Created by XQ on 2017/4/5.
 */
    app.controller("tabsControllerCtrl",function($scope,$rootScope){
        $rootScope.$on('$stateChangeStart',
            function(event, toState, toParams, fromState, fromParams) {

            })
        // stateChangeSuccess  当模板解析完成后触发
        $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
            $('.act').off('infinite')
            $('.sheqy').off('infinite')
            $('.comm').off('infinite')
        })
        $scope.setColor = function (temp) {
            if(temp=="首页"){
                for (var i=1;i<$j4(".isMyOrange").length;i++){
                    $j4(".isMyOrange").removeClass("my_color_orangered")
                }
                $j4("#sy").addClass("my_color_orangered")
            }else if(temp=="活动"){
                for (var i=1;i<$j4(".isMyOrange").length;i++){
                    $j4(".isMyOrange").removeClass("my_color_orangered")
                }
                $j4("#hd").addClass("my_color_orangered")
            }else if(temp=="社区"){
                for (var i=1;i<$j4(".isMyOrange").length;i++){
                    $j4(".isMyOrange").removeClass("my_color_orangered")
                }
                $j4("#sq").addClass("my_color_orangered")
            }else if(temp=="个人中心"){
                for (var i=1;i<$j4(".isMyOrange").length;i++){
                    $j4(".isMyOrange").removeClass("my_color_orangered")
                }
                $j4("#grzx").addClass("my_color_orangered")
            }
        }
    })
    // 主页
    app.controller("HomepageCtrl",function($scope,$http,$compile,$state,getActivityId,getSchool){

        function setColor(temp) {
            if(temp=="首页"){
                for (var i=1;i<$j4(".isMyOrange").length;i++){
                    $j4(".isMyOrange").removeClass("my_color_orangered")
                }
                $j4("#sy").addClass("my_color_orangered")
            }else if(temp=="活动"){
                for (var i=1;i<$j4(".isMyOrange").length;i++){
                    $j4(".isMyOrange").removeClass("my_color_orangered")
                }
                $j4("#hd").addClass("my_color_orangered")
            }else if(temp=="社区"){
                for (var i=1;i<$j4(".isMyOrange").length;i++){
                    $j4(".isMyOrange").removeClass("my_color_orangered")
                }
                $j4("#sq").addClass("my_color_orangered")
            }else if(temp=="个人中心"){
                for (var i=1;i<$j4(".isMyOrange").length;i++){
                    $j4(".isMyOrange").removeClass("my_color_orangered")
                }
                $j4("#grzx").addClass("my_color_orangered")
            }

        }
        setColor($j4("#msy").text())
        // 图片轮播
        var bannerImg = document.getElementsByClassName("bannerImg");
        $scope.autoPlay= function () {
            for (var i = 0; i < bannerImg.length; i++) {
                if (bannerImg[i].children[0].style.display == "block") {
                    n = i;
                    timer = setInterval(function () {
                        n++;
                        //将所有的样式初始化
                        for (var j = 0; j < bannerImg.length; j++) {
                            bannerImg[j].children[0].style.display = "none";
                        }
                        if (n == bannerImg.length) {
                            n = 0;
                            bannerImg[0].children[0].style.display = "block";
                        }
                        if (n < bannerImg.length) {
                            bannerImg[n].children[0].style.display = "block";
                        }
                    }, 1000)
                }
            }
        }

        // 页面初始化
        $scope.SuiInit = function () {
            $.init();
        }

        // 加载学校
        function addSchool(page,size) {
            var http = "http://139.199.71.21:8080/gxjlpt/schools?page="+page+"&size="+size
            $http.get(http).then(function(resp) {
                    if (resp.data.code == 0){
                        $scope.schoolInfoArr = resp.data.data
                    }//加载逻辑问题
            })
        }
        // 预先加载8条
        addSchool(0,8)
        var page = 8;
        // 点击加载其它学校数据
        $scope.HomepageUpdateData = function () {
            console.log(page)
            addSchool(page,8)
            page = document.getElementsByClassName("school_list").length+8;
        }

        // 加载活动
        function addActivity(page,size) {
            var http = "http://139.199.71.21:8080/gxjlpt/activities?page="+page+"&size="+size
            $http.get(http).then(function(resp) {
                if (resp.data.code == 0){
                    $scope.activityInfoArr = resp.data.data
                }
            })
        }
        // 预先加载3条
        addActivity(0,3)
        var page = 3;
        // 点击加载其它学校数据
        $scope.HomepageUpdateActivityData = function () {
            addActivity(page,3)
            page = document.getElementsByClassName("myAct").length+3;
        }

        // 共享活动id
        $scope.shareActivityId = function () {
            $j4(".hotActivity_con").die('click').live("click",function(){
                getActivityId.setActId($j4(this).attr("id"))
            });
        }

        //传递学校名称
        $scope.shareSchoolName = function (data) {
            getSchool.setSchName(data)
        }
        // 搜索
        $scope.searchSchool = null;
        $scope.MySearch = function () {
            getSchool.setSchName($scope.searchSchool)
            $state.go("SchoolActivities")
        }
    })

    // 活动
    app.controller("ActivityCenterCtrl",function($scope,$http,$filter,$compile,$window,$rootScope,$location,getResult,getActivityId){
        function setColor(temp) {
            if(temp=="首页"){
                for (var i=1;i<$j4(".isMyOrange").length;i++){
                    $j4(".isMyOrange").removeClass("my_color_orangered")
                }
                $j4("#sy").addClass("my_color_orangered")
            }else if(temp=="活动"){
                for (var i=1;i<$j4(".isMyOrange").length;i++){
                    $j4(".isMyOrange").removeClass("my_color_orangered")
                }
                $j4("#hd").addClass("my_color_orangered")
            }else if(temp=="社区"){
                for (var i=1;i<$j4(".isMyOrange").length;i++){
                    $j4(".isMyOrange").removeClass("my_color_orangered")
                }
                $j4("#sq").addClass("my_color_orangered")
            }else if(temp=="个人中心"){
                for (var i=1;i<$j4(".isMyOrange").length;i++){
                    $j4(".isMyOrange").removeClass("my_color_orangered")
                }
                $j4("#grzx").addClass("my_color_orangered")
            }

        }
        setColor($j4("#mhd").text())
        $scope.page = 0;
        $scope.activityCountHttp = "http://139.199.71.21:8080/gxjlpt/activities/amount/";
        $scope.activityCollectionHttp = "http://139.199.71.21:8080/gxjlpt/users/collection/";

        // 页面初始化
        $scope.MySuiInit = function () {
            $.init();
        }
        // 页面模板
        $scope.getActivityTemplate = function(info,Ccount,Pcount){
            var imgPath = "http://139.199.71.21:8080/gxjlpt/resource/images/"+info.activityImgPath;
            var imgPath1 = "http://139.199.71.21:8080/gxjlpt/resource/images/"+info.schoolImgPath
            var result = $filter('date')(info.time, 'yyyy-MM-dd');
            $j4(".activityContentList").append($compile("<div class='demo-card-header-pic myActivity bac' id="+info.activityId+" ng-click='setActivityIdFun()'><div valign='bottom' class='card-header color-white no-border no-padding' ><img class='card-cover'"+"src="+imgPath+" style='height: 9rem' ui-sref='EventDetails'></div><div class='card-content-inner myTitle1'><h4 class='color-black my_card_content_font'>"+info.activityTitle+"</h4></div><div class='content-padded grid-demo myLabel'><div class='row no-gutter'><div class='col-60'><a class='link'><i class='fa fa-clock-o' style='color: orangered'></i><span class='mySpanFont'>"+result+"</span></a></div><div class='col-25'><a class='link'><i class='fa fa-star' style='color: #DBDBDB'></i><span class='mySpanFont'>"+Ccount+"</span></a></div><div class='col-15'><a class='link'><i class='fa fa-user' style='color: #DBDBDB'></i><span class='mySpanFont'>"+Pcount+"</span></a></div></div></div><div><div class='myCardFooter'><img src="+imgPath1+" class='myCardFooter_img'><span class='myCardFooter_font'>"+info.schoolName+"</span></div></div></div>")($scope))
        }
        //共享活动id
        $scope.setActivityIdFun = function () {
            $j4(".myActivity").die('click').live("click",function(){
                getActivityId.setActId($j4(this).attr("id"))
            });
        }
        // 下拉加载新的问题
        var loading = false;
        // var maxItems = 100;// 最多可加载的条目
        //向页面添加内容
        $scope.addTopic =function(page, size) {
            var http = "http://139.199.71.21:8080/gxjlpt/activities/all?page="+page+"&size="+size;
            $http.get(http)
                .then(function (resp) {
                    if (resp.data.code == 0){
                        angular.forEach(resp.data.data,function (info) {
                            $scope.activityCountHttpUrl =$scope.activityCountHttp+info.activityId;
                            // 获取收藏人数
                            getResult.get($scope.activityCountHttpUrl).then(function (res) {
                                var count = res;
                                $scope.activityCollectionHttpUrl =$scope.activityCollectionHttp+info.activityId;
                                getResult.get($scope.activityCollectionHttpUrl).then(function (res) {
                                    $scope.getActivityTemplate(info,res,count);
                                })
                            })
                        })

                    }else {
                        $.detachInfiniteScroll($('.infinite-scroll'));
                        // 删除加载提示符
                        $('.infinite-scroll-preloader').remove();
                        return;
                    }
                })

        }
            $scope.addTopic($scope.page, 3);

            $scope.page=3;
            // 注册'infinite'事件处理函数
            $('.act').on('infinite',function() {
                // 如果正在加载，则退出
                if (loading) return;
                // 设置flag
                loading = true;

                // 模拟1s的加载过程
                setTimeout(function() {
                    // 重置加载flag
                    loading = false;
                    // 添加新条目
                    $scope.addTopic($scope.page, 3);
                    // 更新最后加载的序号
                    $scope.page = document.getElementsByClassName("myActivity").length+3;
                    //容器发生改变,如果是js滚动，需要刷新滚动
                    $.refreshScroller();
                }, 1000);

            });
    })

    // 活动详情
    app.controller("EventDetailsCtrl",function($scope,$http,$filter,$state,getActivityId,getUserAccountService,getResult,IsTrue){
        // 活动对象
        $scope.EventDetail = {
            activityTitle:null,
            activityType:null,
            activityStartTime:null,
            activityEndTime:null,
            activityAddress:null,
            activityContent:null,
            activityStatus:null,
            activityImgPath:null
        }

        // 页面初始化
        $scope.MySuiInit = function () {
            $.init();
        }

        //根据id获取活动
        $scope.activityHttp = "http://139.199.71.21:8080/gxjlpt/activities/"+getActivityId.getActId()
        $http.get($scope.activityHttp).then(function (resp) {
            console.log(resp)
            $scope.EventDetail.activityTitle = resp.data.data.activityTitle;
            $scope.EventDetail.activityType = resp.data.data.activityType;
            $scope.EventDetail.activityStartTime = $filter('date')(resp.data.data.activityStartTime, 'yyyy-MM-dd');
            $scope.EventDetail.activityEndTime = $filter('date')(resp.data.data.activityEndTime, 'yyyy-MM-dd');
            $scope.EventDetail.activityAddress = resp.data.data.activityAddress;
            $scope.EventDetail.activityContent = resp.data.data.activityContent;
            $scope.EventDetail.activityStatus = resp.data.data.activityStatus;
            $scope.EventDetail.activityImgPath = "http://139.199.71.21:8080/gxjlpt/resource/images/"+resp.data.data.activityImgPath;
        })

        //查找活动参与人数
        $scope.activityCountHttpUrl ="http://139.199.71.21:8080/gxjlpt/activities/amount/"+getActivityId.getActId();
        getResult.get($scope.activityCountHttpUrl).then(function (resp) {
            $scope.ActivityUserCount = resp;
        })

        //参与活动
        var isJoinHttp = "http://139.199.71.21:8080/gxjlpt/users/takeinfo"
        var joinActivityHttp = "http://139.199.71.21:8080/gxjlpt/users/join"
        $scope.joinInfo = {
            userAccount:getUserAccountService.getAccount(),
            activityId:getActivityId.getActId()
        }
        //判断是否可以参加
        $http({
            method:"post",
            url:isJoinHttp,
            data:$scope.joinInfo
        }).then(function (resp) {
            if (resp.data.code == 16){
                IsTrue.setMyAttr("1")
            }else{
                IsTrue.setMyAttr("0")
            }
        })
        // 参加
        $scope.joinActivityFun = function () {
            if (IsTrue.getMyAttr()==1 && getUserAccountService.getAccount()){
                $http({
                    method:"post",
                    url:joinActivityHttp,
                    data:$scope.joinInfo
                }).then(function (resp) {
                    $.toast("成功！")
                })
            }else if(getUserAccountService.getAccount()==null){
                $.toast("请先登录！")
                $state.go("LogIn")
            }else if(IsTrue.getMyAttr()==0){
                $.toast("你已经参加过本次活动！")
            }
        }

        //收藏活动
        var collectionInfo = {
            activityId:getActivityId.getActId(),
            userAccount:getUserAccountService.getAccount()
        }
        var collectionHttp = "http://139.199.71.21:8080/gxjlpt/users/collection";
        var isCollectionHttp = "http://139.199.71.21:8080/gxjlpt/users/collectinfo";
        $http({
            method:"post",
            url:isCollectionHttp,
            data:collectionInfo
        }).then(function (resp) {
            if (resp.data.code == 16){
                IsTrue.setMyAttr("1")
            }else{
                IsTrue.setMyAttr("0")
                $j4('#isCon').css('color','orangered')
            }
        })

        $scope.collectionFun = function () {
            if (IsTrue.getMyAttr()==1 && getUserAccountService.getAccount()){
                $http({
                    method:"post",
                    url:collectionHttp,
                    data:collectionInfo
                }).then(function (resp) {
                    $j4('#isCon').css('color','orangered')
                    $.toast("收藏成功！")
                })
            }else if(getUserAccountService.getAccount()==null){
                $.toast("请先登录！")
                $state.go("LogIn")
            }
        }
    })

    // 社区
    app.controller("ExchangeCenterCtrl",function($scope,$filter,$http,$compile,getExchangeId){
        function setColor(temp) {
            if(temp=="首页"){
                for (var i=1;i<$j4(".isMyOrange").length;i++){
                    $j4(".isMyOrange").removeClass("my_color_orangered")
                }
                $j4("#sy").addClass("my_color_orangered")
            }else if(temp=="活动"){
                for (var i=1;i<$j4(".isMyOrange").length;i++){
                    $j4(".isMyOrange").removeClass("my_color_orangered")
                }
                $j4("#hd").addClass("my_color_orangered")
            }else if(temp=="社区"){
                for (var i=1;i<$j4(".isMyOrange").length;i++){
                    $j4(".isMyOrange").removeClass("my_color_orangered")
                }
                $j4("#sq").addClass("my_color_orangered")
            }else if(temp=="个人中心"){
                for (var i=1;i<$j4(".isMyOrange").length;i++){
                    $j4(".isMyOrange").removeClass("my_color_orangered")
                }
                $j4("#grzx").addClass("my_color_orangered")
            }

        }
        setColor($j4("#msh").text())
        $scope.page1 = 0;
        $scope.count = 0;

        //定义对象
        $scope.questionInfo = {
            commTitle:null,
            commContent:null,
            commImageOne:null,
            commImageTwo:null,
            commImageThree:null,
            dateTime:null,
            userNickname:null,
            userImgPath:null
        }

        //共享交流问题Id
        $scope.setExchangeContentName = function () {
            $j4(".myExchangeContentCard").die('click').live("click",function(){
                getExchangeId.setId($j4(this).attr("id"))
            });
        }
        // 加载模板
        $scope.getExchangeContentTemplate = function(info){
            var HeadPortrait = "http://139.199.71.21:8080/gxjlpt/resource/images/"+info.userImgPath;
            var commImageOnePath = "http://139.199.71.21:8080/gxjlpt/resource/images/"+info.commImageOne;
            var commImageTwoPath = "http://139.199.71.21:8080/gxjlpt/resource/images/"+info.commImageTwo;
            var commImageThreePath = "http://139.199.71.21:8080/gxjlpt/resource/images/"+info.commImageThree;
            //格式化时间
            var result = $filter('date')(info.dateTime, 'yyyy-MM-dd');
            $j4(".ExchangeContentList").append($compile("<div class='card myExchangeContentCard' id="+info.commId+" ui-sref='Comment' ng-click='setExchangeContentName()'><div class='card-content'><div class='list-block media-list'><ul><li class='item-content'><div class='item-media'><img src="+HeadPortrait+" width='44'></div><div class='item-inner'><div class='item-title-row'><div class='item-title exchangeCenter_font'>"+info.userNickname+"</div></div><div class='item-subtitle exchangeCenter_title' style='margin-top: 0.6rem'>"+info.commTitle+"</div></div></li></ul><div class='question_content'><div style='font-family: SimSun;font-size: small;font-weight: 500;margin-left: 0.6rem;margin-right: 0.6rem;'>"+info.commContent+"</div><div class='question_content_img' style='margin-bottom: 0.2rem;margin-left: 0.6rem;'><img src="+commImageOnePath+" style='width: 3rem;height: 3rem;margin-right: 0.6rem;' /><img src="+commImageTwoPath+" style='width: 3rem;height: 3rem;margin-right: 0.6rem;' /><img src="+commImageThreePath+" style='width: 3rem;height: 3rem;margin-right: 0.6rem;' /></div></div></div></div><div class='card-footer'><span>"+result+"</span><span>评论</span></div></div>")($scope))
        }

        // 页面初始化
        $scope.MySuiInit = function () {
            $.init();
        }
        //下拉加载新的问题
        var loading1 = false;

        //向页面添加内容
        function addExchangeContent(page, size) {
            var http = "http://139.199.71.21:8080/gxjlpt/users/comm?page="+page+"&size="+size;
            $http.get(http)
                .then(function (resp) {
                    if (resp.data.code == 0){
                        angular.forEach(resp.data.data,function (info) {
                            $scope.getExchangeContentTemplate(info);
                        })
                    }else {
                        $.detachInfiniteScroll($('.infinite-scroll'));
                        // 删除加载提示符
                        $('.infinite-scroll-preloader').remove();
                        return;
                    }
            })
        }

        //预先加载5条
        addExchangeContent($scope.page1,3)

        $scope.page1 =3;// 第二页页码

        // 注册'infinite'事件处理函数

            $('.shequ').on('infinite', function() {
                if(loading1) return;// 如果正在加载，则退出

                loading1 = true;// 设置flag

                // 模拟1s的加载过程
                setTimeout(function() {
                    // 重置加载flag
                    loading1 = false;

                    // 添加新条目
                    addExchangeContent($scope.page1, 3);

                    // 更新最后加载的序号
                    $scope.page1 = document.getElementsByClassName("myExchangeContentCard").length+3;
                    // 容器发生改变,如果是js滚动，需要刷新滚动
                    $.refreshScroller();
                }, 1000);
            });


    })

    // 评论
    app.controller("CommentCtrl",function($scope,$http,$state,$filter,$compile,getExchangeId,getActivityId,getUserAccountService){
        var imgPre = "http://139.199.71.21:8080/gxjlpt/resource/images/"
        $scope.CommentInfo = {
            commentId:null,
            commentContent:null,
            time:null,
            commTitle:null,
            commContent:null,
            commImageOne:null,
            commImageTwo:null,
            commImageThree:null
        }
        //页面初始化
        $scope.MySuiInit = function () {
            $.init();
        }
        //获取所要评论的问题
        var CommHttp = "http://139.199.71.21:8080/gxjlpt/communications/"+getExchangeId.getId()

        //获取所要评论的内容
        $http.get(CommHttp).then(function (resp) {

            $scope.CommentInfo.commTitle = resp.data.data.commTitle;
            $scope.CommentInfo.commContent = resp.data.data.commContent;
            $scope.CommentInfo.commImageOne = imgPre+resp.data.data.commImageOne;
            $scope.CommentInfo.commImageTwo = imgPre+resp.data.data.commImageTwo;
            $scope.CommentInfo.commImageThree = imgPre+resp.data.data.commImageThree;
        })

        //加载模板
        $scope.getReviewTemplate = function(info){
            var imgPath = imgPre+info.userImgPath
            $j4(".Comment_List").append($compile("<div class='Comment_con_list'><div class='card' style='box-shadow: none'><div class='card-content'><div class='list-block media-list'><ul ><li class='item-content'><div class='item-media'><img src="+imgPath+" style='width: 44px;height: 44px;border-radius: 22px'></div><div class='item-inner'><div class='item-title-row'><div class='item-title reviewer'>"+info.userNickname+"</div></div><div class='item-subtitle review_where'><span>"+$filter('date')(info.time, 'yyyy-MM-dd')+"</span></div></div></li></ul></div></div><div class='Comment_con_footer'><span>"+info.commentContent+"</span></div></div></div>")($scope))
        }

        // 下拉加载新的问题
        var loading = false;

        var page = 0;// 第一页页码

        //向页面添加内容
        function addReview(page, size) {
            var reviewHttp = "http://139.199.71.21:8080/gxjlpt/comments/"+getExchangeId.getId()+"/communcation?page="+page+"&size="+size
            $http.get(reviewHttp)
                .then(function (resp) {
                    if (resp.data.code == 0){
                        if (resp.data.data.length == 0){
                            $.toast("已加所有评论！");
                            // 加载完毕，则注销无限加载事件，以防不必要的加载
                            $.detachInfiniteScroll($('.infinite-scroll'));
                            // 删除加载提示符
                            $('.infinite-scroll-preloader').remove();
                            return;
                        }else{
                            angular.forEach(resp.data.data,function (info) {
                                $scope.getReviewTemplate(info);
                            })
                        }
                    }
                })
        }

        //预先加载5条
        addReview(page, 5);

        var page = 5;// 第二页页码

        // 注册'infinite'事件处理函数
        $j4('.comm').on('infinite', function() {

            if(loading) return;// 如果正在加载，则退出

            loading = true;// 设置flag

            // 模拟1s的加载过程
            setTimeout(function() {
                // 重置加载flag
                loading = false;
                // 添加新条目
                addReview(page, 5);
                // 更新最后加载的序号
                page = document.getElementsByClassName("Comment_con_list").length+5;
                //容器发生改变,如果是js滚动，需要刷新滚动
                $.refreshScroller();
            }, 1000);
        });

        // 上传评论内容
        $scope.commentConInfo = {
            commentContent:null,
            userAccount:getUserAccountService.getAccount(),
            commId:getExchangeId.getId()
        };
        $scope.commentFun = function () {
            if(getUserAccountService.getAccount()){
                $http({
                    method:"post",
                    url:"http://139.199.71.21:8080/gxjlpt/users/comment",
                    data:$scope.commentConInfo
                }).then(function (resp) {
                    if (resp.data.code == 0){
                        $scope.commentConInfo.commentContent = null
                    }else {
                        $.toast(resp.msg);
                    }
                })
            }else{
                $.toast("请先登录！")
                $state.go("LogIn")
            }
        }
    })

    // 个人中心
    app.controller("PersonalCenterCtrl",function($scope,$http,getUserAccountService){
        function setColor(temp) {
            if(temp=="首页"){
                for (var i=1;i<$j4(".isMyOrange").length;i++){
                    $j4(".isMyOrange").removeClass("my_color_orangered")
                }
                $j4("#sy").addClass("my_color_orangered")
            }else if(temp=="活动"){
                for (var i=1;i<$j4(".isMyOrange").length;i++){
                    $j4(".isMyOrange").removeClass("my_color_orangered")
                }
                $j4("#hd").addClass("my_color_orangered")
            }else if(temp=="社区"){
                for (var i=1;i<$j4(".isMyOrange").length;i++){
                    $j4(".isMyOrange").removeClass("my_color_orangered")
                }
                $j4("#sq").addClass("my_color_orangered")
            }else if(temp=="个人中心"){
                for (var i=1;i<$j4(".isMyOrange").length;i++){
                    $j4(".isMyOrange").removeClass("my_color_orangered")
                }
                $j4("#grzx").addClass("my_color_orangered")
            }

        }
        setColor($j4("#mgrzx").text())
        // 初始化页面
        $scope.MySuiInit = function () {
            $.init();
        }
        // 得到用户头像
        var getHeadPortraitHttp = "http://139.199.71.21:8080/gxjlpt/users/account/"+getUserAccountService.getAccount()
        if(getUserAccountService.getAccount()){
            $http.get(getHeadPortraitHttp).then(function (resp) {
                $scope.PersonalCenterImgPath = "http://139.199.71.21:8080/gxjlpt/resource/images/"+resp.data.data.userImgPath
                if (resp.data.data.userImgPath == null){

                    $j4(".MPImg").attr('src','img/woman.jpg');
                    $j4("#isLog").text(resp.data.data.userNickname)
                }else{

                    $j4(".MPImg").attr('src',$scope.PersonalCenterImgPath);
                    $j4("#isLog").text(resp.data.data.userNickname)
                }

            })
        }

        //登出操作
        $scope.LogOut = function () {
            $j4(".MPImg").attr('src','img/woman.jpg');
            $j4("#isLog").text("立即登录")
            getUserAccountService.setAccount("");
        }

    })

    // 个人资料
    app.controller("PersonalDataCtrl",function($scope,$http,getUserAccountService){
        // 页面初始化
        $scope.MySuiInit = function () {
            $.init();
        }
        var http = "http://139.199.71.21:8080/gxjlpt/users/account/"+getUserAccountService.getAccount()
        if (getUserAccountService.getAccount()){
            $http.get(http).then(function (resp) {
                $scope.personalInfo = resp.data.data;
                if($scope.personalInfo.userImgPath == null){
                    $scope.personalImgPath = "http://139.199.71.21:8080/gxjlpt/resource/images/"+$scope.personalInfo.userImgPath;
                }
            })
        }
        // 上传活动图片
        $scope.UploadUserHeader = function () {
            var uploadPath = "http://139.199.71.21:8080/gxjlpt/users/upload/"+getUserAccountService.getAccount();
            var fd = new FormData();
            var file = document.getElementById("fi").files[0];
            fd.append('file', file);
            if (file != null){
                $http({
                    method:'POST',
                    url:uploadPath,
                    data: fd,
                    headers: {'Content-Type':undefined},
                }).then(function success (resp) {
                    if (resp.data.code == 0){
                        $.toast("上传成功!")
                    }else{
                        $.toast(resp.data.msg);
                    }
                })
            }
        }

    })

    // 发布活动
    app.controller("ReleaseActivitiesCtrl",function($scope,$http,$state,getUserAccountService){
        $scope.uploadPath = null;
        $scope.uploadPrefix = "http://139.199.71.21:8080/gxjlpt/activities/upload/";
        // 活动对象
        $scope.activityInfo = {
            activityTitle:null,
            activityAddress:null,
            activityStartTime:null,
            activityEndTime:null,
            activityType:null,
            activityContent:null,
            activityStatus:"正在进行",
            userAccount:getUserAccountService.getAccount()
        }
        // 页面初始化
        $scope.MySuiInit = function () {
            $.init();
        }

        // 发布活动
        $scope.releaseActivity = function () {
            if(getUserAccountService.getAccount()){
                // 上传活动
                $http({
                    method:"post",
                    url:"http://139.199.71.21:8080/gxjlpt/users/make",
                    data:$scope.activityInfo
                }).then(function (resp) {
                    // 成功上传
                    if (resp.data.code == 0){
                        console.log(resp.data.data.activityId);
                        // 上传活动图片
                        var uploadPer = "http://139.199.71.21:8080/gxjlpt/activities/upload/"
                        var uploadPath = uploadPer+resp.data.data.activityId;
                        var fd = new FormData();
                        var file = document.getElementById("fi").files[0];
                        fd.append('file', file);
                        $http({
                            method:'POST',
                            url:uploadPath,
                            data: fd,
                            headers: {'Content-Type':undefined},
                        }).then(function success (resp) {
                            if (resp.data.code == 0){
                                $.toast("发布成功")
                                $state.go("tabsController.ActivityCenter",{reload:true})
                            }else{
                                $.toast(resp.data.msg);
                            }
                        })
                    }else{
                        $.toast(resp.data.msg);
                    }
                })
            }else {
                $.toast("请先登录！")
                $state.go("LogIn")
            }


        }
    })

    // 登录页面
    app.controller("LogInCtrl",function($scope,$http,$state,getUserAccountService){
        $scope.userLogInInfo = {
            account:getUserAccountService.getAccount(),
            password:null
        }

        // 页面初始化
        $scope.MySuiInit = function () {
            $.init();
        }

        // 登录功能
        $scope.LogInFun = function () {
            $http({
                method:"post",
                url:"http://139.199.71.21:8080/gxjlpt/users/login",
                data:$scope.userLogInInfo
            }).then(function (resp) {
                if (resp.data.code == 0){
                    getUserAccountService.setAccount($scope.userLogInInfo.account)
                    $state.go("tabsController.PersonalCenter")
                }else if(resp.data.code == 9){
                    $.toast(resp.data.msg);
                }
            })
        }
    })

    // 注册页面
    app.controller("RegisterCtrl", function($scope,$http,$state,getUserAccountService) {
        $scope.MySuiInit = function () {
            $.init();
        }
        $scope.userInfo = {
            userAccount: null,
            userPassword: null,
            userNickname: null,
            schoolName: null
        }
        $scope.UserRegister = function () {
            $http({
                method: "post",
                url: "http://139.199.71.21:8080/gxjlpt/users",
                data:$scope.userInfo
            }).then(function(resp) {
                if (resp.data.code == 0){
                    $.toast("注册成功");
                    getUserAccountService.setAccount($scope.userInfo.userAccount)
                    $state.go("LogIn");
                }else{
                    $.toast("注册失败，该用户已存在！")
                }
            })
        }
    })

    // 学校活动
    app.controller("SchoolActivitiesCtrl",function($scope,$http,$state,getSchool,getActivityId){
        // 页面初始化
        $scope.MySuiInit = function () {
            $.init();
        }
        //广播学校活动Id
        $scope.setSchoolActivityId = function () {
            $j4(".MSL").die('click').live("click",function(){
                getActivityId.setActId($j4(this).attr("id"))
                $state.go("EventDetails")
            });
        }
        
        // 获取学校活动
        $scope.schoolName1 = getSchool.getSchName();
        $scope.schoolActInfoArr =null;
        var schoolNameHttp = "http://139.199.71.21:8080/gxjlpt/activities/school/"+getSchool.getSchName()
        $http.get(schoolNameHttp).then(function (resp) {
            $scope.schoolActInfoArr = resp.data.data

        })
        //获取所有的学校名称和图片
        var schoolInfoHttp1 = "http://139.199.71.21:8080/gxjlpt/schools?page=0&size=12"
        $http.get(schoolInfoHttp1).then(function (resp) {
            console.log(resp.data.data)
            $scope.schoolInfoArr1 = resp.data.data
        })

        //点击学校获取数据
        $scope.schoolClick = function (schTemp) {
            $scope.schoolName1 = schTemp
            var schoolNameHttp = "http://139.199.71.21:8080/gxjlpt/activities/school/"+schTemp
            $http.get(schoolNameHttp).then(function (resp) {
                $scope.schoolActInfoArr = resp.data.data
            })
        }
    })

    // 发布话题
    app.controller("IssueTopicCtrl",function($scope,$state,$http,getUserAccountService){
        $scope.topicInfo = {
            commTitle:null,
            commContent:null,
            userAccount:getUserAccountService.getAccount()
        }

        // 页面初始化
        $scope.MySuiInit = function () {
            $.init();
        }

        if(getUserAccountService.getAccount()){
            // 发布功能
            $scope.IssueTopicFun = function () {
                $http({
                    method:'POST',
                    url:"http://139.199.71.21:8080/gxjlpt/users/comm",
                    data:$scope.topicInfo
                }).then(function success (resp) {
                    if (resp.data.code == 0){
                        console.log(resp);
                        var topicUrl ="http://139.199.71.21:8080/gxjlpt/communication/upload/"+ resp.data.data.commId;
                        // 上传图片
                        var fd = new FormData();
                        var file = document.getElementById("f").files[0];
                        var file01 = document.getElementById("f1").files[0];
                        var file02 = document.getElementById("f2").files[0];
                        fd.append('files', file);
                        fd.append('files', file01);
                        fd.append('files', file02);
                        $http({
                            method:'POST',
                            url:topicUrl,
                            data: fd,
                            headers: {'Content-Type':undefined},
                        }).then(function success (resp) {
                            $state.go("tabsController.PersonalCenter")
                        })
                    }else{
                        $.toast(resp.data.msg);
                    }
                })

            }
        }else{

            $.toast("请先登录！")
            $state.go("LogIn")
        }

    })

    // 修改个人资料
    app.controller("ModifyPersonalDataCtrl",function($scope,$http,$state,getUserAccountService){
        $scope.UserUploadInfo = {
            userNickname:null,
            userSex:null,
            userImgPath:null,
            userPassword:null,
            schoolName:null,
            userEmail:null,
            userAccount:getUserAccountService.getAccount()
        }

        // 页面初始化
        $scope.MySuiInit = function () {
            $.init();
        }

        // 获取修改的信息
        var ModifyPersonalDataHttp = "http://139.199.71.21:8080/gxjlpt/users/account/"+getUserAccountService.getAccount()
        if (getUserAccountService.getAccount()){
            $http.get(ModifyPersonalDataHttp)
                .then(function (resp) {
                    $scope.UserUploadInfo.userNickname = resp.data.data.userNickname;
                    $scope.UserUploadInfo.userSex = resp.data.data.userSex;
                    $scope.UserUploadInfo.userImgPath = resp.data.data.userImgPath;
                    $scope.UserUploadInfo.userPassword = resp.data.data.userPassword;
                    $scope.UserUploadInfo.schoolName = resp.data.data.schoolName;
                    $scope.UserUploadInfo.userEmail = resp.data.data.userEmail;
                    $scope.UserUploadInfo.userAccount = resp.data.data.userAccount;
                })
            // 修改信息
            $scope.uploadPersonalInfo = function () {
                $http({
                    method:"put",
                    url:"http://139.199.71.21:8080/gxjlpt/users",
                    data:$scope.UserUploadInfo
                }).then(function (resp) {
                    $.toast("修改成功")
                    $state.go("PersonalData",{reload: true})
                })
            }
        }

    })

    // 我的活动
    app.controller("MyActivitiesCtrl",function($scope,$http,$compile,$state,getActivityId,getUserAccountService,$filter){
        // 初始化页面
        $scope.MySuiInit = function () {
            $.init();
        }

        //广播Id
        $scope.setMyActivityId = function () {
            $j4(".MyActivityId").die('click').live("click",function(){
                getActivityId.setActId($j4(this).attr("id"))
                $state.go("EventDetails")
            });
        }

        // 页面模板
        $scope.getMyActivityTemplate = function (info) {
            var MyActivityStartTime = $filter('date')(info.activityStartTime, 'yyyy/MM/dd');
            var MyActivityEndTime = $filter('date')(info.activityEndTime, 'yyyy/MM/dd');
            $j4(".MyActivityList").append($compile("<div class='card SchAct-public-card MyActivityId' ng-click='setMyActivityId()' id='"+info.activityId+"' style='margin-top:0.4rem'><div class='card-content'><div class='list-block media-list SchAct-public-card-bac'><ul><li class='item-content'><div class='item-media'><img src='http://gqianniu.alicdn.com/bao/uploaded/i4//tfscom/i3/TB10LfcHFXXXXXKXpXXXXXXXXXX_!!0-item_pic.jpg_250x250q60.jpg' width='100'></div><div class='item-inner SchAct-fr'><div class='item-title-row SchAct-pub-head'><div class='item-title SchAct-head-word'>"+info.activityTitle+"</div></div><div class='item-subtitle SchAct-two-head-word'>地点："+info.activityAddress+"</div><div class='item-subtitle SchAct-two-head-word'>时间："+MyActivityStartTime+"-"+MyActivityEndTime+"</div></div></li></ul></div></div></div>")($scope))
        }
        // 获取用户发布过的活动
        var loading = false;

        var page = 0;// 第一页页码

        //向页面添加内容
        if (getUserAccountService.getAccount()){
            function addActivity(page, size) {
                var MyActivitiesHttp = "http://139.199.71.21:8080/gxjlpt/activities/usermake/"+getUserAccountService.getAccount()+"?page="+page+"&size="+size;
                $http.get(MyActivitiesHttp)
                    .then(function (resp) {
                        if (resp.data.code == 0){
                            angular.forEach(resp.data.data,function (info) {
                                $scope.getMyActivityTemplate(info)
                            })
                        }else {
                            // 加载完毕，则注销无限加载事件，以防不必要的加载
                            $.detachInfiniteScroll($('.infinite-scroll'));
                            // 删除加载提示符
                            $('.infinite-scroll-preloader').remove();
                            return;
                        }
                    })
            }
            //预先加载5条
            addActivity(page, 5);
        }else{
            $('.infinite-scroll-preloader').remove();
        }




        var page = 5;// 第二页页码

        // 注册'infinite'事件处理函数
        $(document).on('infinite', function() {

            if(loading) return;// 如果正在加载，则退出

            loading = true;// 设置flag

            // 模拟1s的加载过程
            setTimeout(function() {
                // 重置加载flag
                loading = false;

                if(page >= maxItems) {
                    // 加载完毕，则注销无限加载事件，以防不必要的加载
                    $.detachInfiniteScroll($('.infinite-scroll'));
                    // 删除加载提示符
                    $('.infinite-scroll-preloader').remove();
                    return;
                }
                // 添加新条目
                addActivity(page, 5);
                // 更新最后加载的序号
                page = document.getElementsByClassName("card").length+5;
                //容器发生改变,如果是js滚动，需要刷新滚动
                $.refreshScroller();
            }, 1000);
        });
    })

    // 我的话题
    app.controller("MyTopicCtrl",function($scope,$http,getUserAccountService){
        // 页面初始化
        $scope.MySuiInit = function () {
            $.init();
        }
        var http = "http://139.199.71.21:8080/gxjlpt/users/comminfo/"+getUserAccountService.getAccount()
        //获取我自己的话题
        $http.get(http)
            .then(function (resp) {
                if (resp.data.code == 0){
                    console.log(resp)
                    $scope.MyTopicInfoArr = resp.data.data
                }
        })
    })

    //我的足迹
    app.controller("ParticipatedInActivitiesCtrl",function($scope,$http){
        //初始化页面
        $scope.MySuiInit = function () {
            $.init();
        }

    })
    // 我收藏的活动
    app.controller("CollectionActivitiesCtrl",function($scope,$http,getUserAccountService,getActivityId,$state){
        // 初始化页面
        $scope.MySuiInit = function () {
            $.init();
        }
        //广播收藏活动Id
        $scope.setCollectionActivities = function () {
            $j4(".MCA").die('click').live("click",function(){
                getActivityId.setActId($j4(this).attr("id"))
                $state.go("EventDetails")
            });
        }
        //获取收藏的活动
        var CollectionActivitiesHttp = "http://139.199.71.21:8080/gxjlpt/users/collection/info/"+getUserAccountService.getAccount();
        $http.get(CollectionActivitiesHttp)
            .then(function (resp) {
            $scope.CollectionActivitiesArr = resp.data.data
        })
    })
    app.controller("UploadSchoolInformationCtrl",function($scope,$http){
        $scope.MySuiInit = function () {
            $.init();
        }

    })


